import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { ReuseTabService } from '@delon/abc';
import { NzMessageService } from 'ng-zorro-antd';
import { HttpClient } from '@angular/common/http';
import { environment } from '../../../../environments/environment';
import { ExportFileService } from '../../../core/exportFile';
import { TableComponent } from './table.component';

@Component({
    selector: 'app-tectonicsynthesis',
    templateUrl: './tectonicSynthesis.component.html',
    styleUrls: ['./tectonicSynthesis.component.less']
})
export class TectonicSynthesisComponent implements OnInit {
    @ViewChild('tab', { static: true }) tab: TableComponent;
    pageType = '';
    headData = [];
    paramType = '';
    title = '';
    addModalVisible = false;
    delModalVisible = false;
    addLoading = false;
    delLoading = false;
    tempData: any = [];
    exportUrl = '';

    constructor(
        private activatedRoute: ActivatedRoute,
        private reuseTabService: ReuseTabService,
        private router: Router,
        private msg: NzMessageService,
        private exportFile: ExportFileService,
        private http: HttpClient,
    ) { }

    ngOnInit() {
        this.activatedRoute.params.subscribe((params: Params) => {
            this.pageType = params.type;
            switch (this.pageType) {
                case 'safetyAnalyse':
                    this.title = '安全数据网分析';
                    this.reuseTabService.title = this.title;
                    this.paramType = '4';
                    break;
                case 'rotaryAnalyse':
                    this.title = '动环分析';
                    this.reuseTabService.title = this.title;
                    this.paramType = '3';
                    break;
                case 'dmsAnalyse':
                    this.title = 'DMS分析';
                    this.reuseTabService.title = this.title;
                    this.paramType = '1';
                    break;
                case 'lkjAnalyse':
                    this.title = 'LKJ分析';
                    this.reuseTabService.title = this.title;
                    this.paramType = '6';
                    break;
                case 'eoasAnalyse':
                    this.title = 'EOAS视频分析';
                    this.reuseTabService.title = this.title;
                    this.paramType = '2';
                    break;
                case 'videoAnalyse':
                    this.title = '综合视频分析';
                    this.reuseTabService.title = this.title;
                    this.paramType = '5';
                    break;
                default:
                    this.router.navigate(['exception/404']);
                    break;
            }
            this.loadList('');
        });
    }
    loadList(params) {
        this.exportUrl = params + '&type=' + this.paramType;
        this.tab.loadList(environment.project + '/syntheticAnalysis/query', params + '&type=' + this.paramType);
    }
    addList() {
        this.addLoading = true;
        this.tab.addList(() => {
            this.addLoading = false;
        });
    }
    delconfirm() {
        this.delLoading = true;
        let delStatus = false;
        let objData = [];
        const delData = { 'count': 0, 'id': [] };
        const tableData = this.tab.getAllData();
        for (const item of tableData) {
            if (item.checked) {
                objData = item;
                delData.count += 1;
                delData.id.push(item.id)
                delStatus = true;
            }
        }
        if (delStatus) {
            const url = environment.project + '/syntheticAnalysis/delete';
            this.http.post(url, delData.id).subscribe((res: any) => {
                setTimeout(() => {
                    if (!res.code) {
                        this.loadList('');
                    } else {
                        this.msg.error(res.msg);
                    }
                    this.delLoading = false;
                }, 500);
            });
        } else {
            this.msg.error('请选择数据删除');
        }
    }
    delCancel() {
        // to do
    }
    export() {
        this.exportFile.getDown(environment.project + '/syntheticAnalysis/export?page=1&limit=20' + this.exportUrl, '综合分析' + this.title + '.xls');
    }
}
